package com.ruoyi.yscggl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.yscggl.domain.bo.JfsbmxBo;
import com.ruoyi.yscggl.domain.vo.JfsbmxVo;
import com.ruoyi.yscggl.domain.Jfsbmx;
import com.ruoyi.yscggl.mapper.JfsbmxMapper;
import com.ruoyi.yscggl.service.IJfsbmxService;

import java.util.List;
import java.util.Collection;

/**
 * 经费申报明细Service业务层处理
 *
 * @author jiayuan
 * @date 2023-04-20
 */
@RequiredArgsConstructor
@Service
public class JfsbmxServiceImpl implements IJfsbmxService {

    private final JfsbmxMapper baseMapper;

    /**
     * 查询经费申报明细
     */
    @Override
    public JfsbmxVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询经费申报明细列表
     */
    @Override
    public TableDataInfo<JfsbmxVo> queryPageList(JfsbmxBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Jfsbmx> lqw = buildQueryWrapper(bo);
        Page<JfsbmxVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询经费申报明细列表
     */
    @Override
    public List<JfsbmxVo> queryList(JfsbmxBo bo) {
        LambdaQueryWrapper<Jfsbmx> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Jfsbmx> buildQueryWrapper(JfsbmxBo bo) {
        LambdaQueryWrapper<Jfsbmx> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPnNo()), Jfsbmx::getPnNo, bo.getPnNo());
        lqw.like(StringUtils.isNotBlank(bo.getPnName()), Jfsbmx::getPnName, bo.getPnName());
        lqw.eq(StringUtils.isNotBlank(bo.getAsNo()), Jfsbmx::getAsNo, bo.getAsNo());
        lqw.like(StringUtils.isNotBlank(bo.getAsName()), Jfsbmx::getAsName, bo.getAsName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptNo()), Jfsbmx::getDeptNo, bo.getDeptNo());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), Jfsbmx::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), Jfsbmx::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getSpec()), Jfsbmx::getSpec, bo.getSpec());
        lqw.eq(bo.getAnCount() != null, Jfsbmx::getAnCount, bo.getAnCount());
        lqw.eq(StringUtils.isNotBlank(bo.getUnits()), Jfsbmx::getUnits, bo.getUnits());
        lqw.eq(bo.getPriceCny() != null, Jfsbmx::getPriceCny, bo.getPriceCny());
        lqw.eq(bo.getBudgetAmount() != null, Jfsbmx::getBudgetAmount, bo.getBudgetAmount());
        return lqw;
    }

    /**
     * 新增经费申报明细
     */
    @Override
    public Boolean insertByBo(JfsbmxBo bo) {
        Jfsbmx add = BeanUtil.toBean(bo, Jfsbmx.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改经费申报明细
     */
    @Override
    public Boolean updateByBo(JfsbmxBo bo) {
        Jfsbmx update = BeanUtil.toBean(bo, Jfsbmx.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Jfsbmx entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除经费申报明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
